/******************************************************************************
 *                       B O M B E R M A N  2 0 1 2
 * 
 * File Name: ClientSocket.java
 * 
 * COP 4331 Summer 2012 Project
 * 
 * Authors: Trevor Mancuso, Chris Chiarella, Zach Hoyler, Daniel Martin
 * 
 * 
 * Description: Socket connection for the client.
 * 
 ******************************************************************************/
package bomberman;

import java.io.IOException;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;


public class ClientSocket extends GameSocket implements Runnable{
    
    DatagramSocket clientSocket = null;
    DatagramSocket clientListenSocket = null;
    boolean running = true;
    //InetAddress serverAddress = null;
    Bomberman game;
    byte[] sendData = new byte[1024]; 
    byte[] receiveData = new byte[1024]; 
    
    //Constructor
    ClientSocket(Bomberman game) throws SocketException, UnknownHostException
    {
        clientSocket = new DatagramSocket();
        clientListenSocket = new DatagramSocket(4320);
        //serverAddress = InetAddress.getByName(server);
        this.game = game;
        
        
    }
    
    //When in client mode, listen on Port 4320 and send on port 4321
    public void run()
    {
        while(running)
        {
            try {
                listen();
            } catch (IOException ex) {
                Logger.getLogger(ClientSocket.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
        return;
    }
    //Constantly listen, if a message is received, pass it to the NetworkHandler
    public void listen() throws IOException
    {
        receiveData = new byte[1024]; 

          DatagramPacket receivePacket = 
             new DatagramPacket(receiveData, receiveData.length); 

          System.out.println ("Waiting for datagram packet");

          try{
            clientListenSocket.receive(receivePacket); 
          }catch(SocketException ex)
          {
              stopRunning();
              closeSockets();
          }

          String sentence = new String(receivePacket.getData()); 
  
          InetAddress IPAddress = receivePacket.getAddress(); 
  
          int port = receivePacket.getPort(); 
          
          game.handler.handleMessage(sentence.trim());
          
          System.out.println("CLIENT RECIEVED");
          System.out.println ("From: " + IPAddress + ":" + port);
          System.out.println ("Message: " + sentence.trim());
    }
    
    @Override
    //Send a message over UDP
    public void sendMessage(String message,String address) throws UnknownHostException, IOException
    {
        sendData = message.getBytes();
        
        System.out.println ("Sending data to " + sendData.length + 
                          " bytes to server.");
        
        DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length,InetAddress.getByName(address),4321);
        
        clientSocket.send(sendPacket);
    }
    //close the socket
    public void closeSockets()
    {
        clientSocket.close();
        clientListenSocket.close();
    }
    //Stop the listening thread
    public void stopRunning()
    {
        running = false;
    }
    
    
}
